package com.ruoyi.app.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.AppMediumActivityUserMapper;
import com.ruoyi.app.domain.AppMediumActivityUser;
import com.ruoyi.app.service.IAppMediumActivityUserService;

/**
 * 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-04
 */
@Service
public class AppMediumActivityUserServiceImpl implements IAppMediumActivityUserService
{
    @Autowired
    private AppMediumActivityUserMapper appMediumActivityUserMapper;

    /**
     * 查询活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     *
     * @param activityUserId 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）主键
     * @return 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     */
    @Override
    public AppMediumActivityUser selectAppMediumActivityUserByActivityUserId(Long activityUserId)
    {
        return appMediumActivityUserMapper.selectAppMediumActivityUserByActivityUserId(activityUserId);
    }

    /**
     * 查询活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）列表
     *
     * @param appMediumActivityUser 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     * @return 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     */
    @Override
    public List<AppMediumActivityUser> selectAppMediumActivityUserList(AppMediumActivityUser appMediumActivityUser)
    {
        return appMediumActivityUserMapper.selectAppMediumActivityUserList(appMediumActivityUser);
    }
    /**
     * 统计指定活动的报名人数
     */
    @Override
    public int countEnrolledUsersByActivityId(Long activityId) {
        return appMediumActivityUserMapper.countEnrolledUsersByActivityId(activityId);
    }
    /**
     * 新增活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     *
     * @param appMediumActivityUser 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     * @return 结果
     */
    @Override
    public int insertAppMediumActivityUser(AppMediumActivityUser appMediumActivityUser)
    {
        return appMediumActivityUserMapper.insertAppMediumActivityUser(appMediumActivityUser);
    }

    /**
     * 修改活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     *
     * @param appMediumActivityUser 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     * @return 结果
     */
    @Override
    public int updateAppMediumActivityUser(AppMediumActivityUser appMediumActivityUser)
    {
        return appMediumActivityUserMapper.updateAppMediumActivityUser(appMediumActivityUser);
    }

    /**
     * 批量删除活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）
     *
     * @param activityUserIds 需要删除的活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）主键
     * @return 结果
     */
    @Override
    public int deleteAppMediumActivityUserByActivityUserIds(Long[] activityUserIds)
    {
        return appMediumActivityUserMapper.deleteAppMediumActivityUserByActivityUserIds(activityUserIds);
    }

    /**
     * 删除活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）信息
     *
     * @param activityUserId 活动与用户的关联关系（无物理外键，通过业务逻辑维护关联）主键
     * @return 结果
     */
    @Override
    public int deleteAppMediumActivityUserByActivityUserId(Long activityUserId)
    {
        return appMediumActivityUserMapper.deleteAppMediumActivityUserByActivityUserId(activityUserId);
    }


    /**
     * 用户报名活动
     */
    @Override
    public int enrollActivity(Long userId, Long activityId) {
        // 参数校验
        if (userId == null || activityId == null) {
            throw new IllegalArgumentException("用户ID和活动ID不能为空");
        }

        // 查询已有关联记录
        AppMediumActivityUser existing = appMediumActivityUserMapper.selectByUserIdAndActivityId(userId, activityId);
        Date now = new Date();

        if (existing == null) {
            // 无记录：新增报名（仅参与）
            AppMediumActivityUser newRecord = new AppMediumActivityUser();
            newRecord.setUserId(userId);
            newRecord.setActivityId(activityId);
            newRecord.setStatusId(1L); // 1=仅参与
            newRecord.setJoinTime(now);
            appMediumActivityUserMapper.insertAppMediumActivityUser(newRecord);
            return 1; // 新增
        } else {
            // 有记录：更新状态（若已收藏则改为3，否则改为1）
            Long newStatus = (existing.getStatusId() == 2L) ? 3L : 1L; // 2=仅收藏→3=既参与且收藏
            existing.setStatusId(newStatus);
            existing.setJoinTime(now); // 更新参与时间
            appMediumActivityUserMapper.updateStatusByUserIdAndActivityId(existing);
            return 2; // 更新
        }
    }

    /**
     * 用户收藏活动
     */
    @Override
    public int collectActivity(Long userId, Long activityId) {
        // 参数校验
        if (userId == null || activityId == null) {
            throw new IllegalArgumentException("用户ID和活动ID不能为空");
        }

        // 查询已有关联记录
        AppMediumActivityUser existing = appMediumActivityUserMapper.selectByUserIdAndActivityId(userId, activityId);
        Date now = new Date();

        if (existing == null) {
            // 无记录：新增收藏（仅收藏）
            AppMediumActivityUser newRecord = new AppMediumActivityUser();
            newRecord.setUserId(userId);
            newRecord.setActivityId(activityId);
            newRecord.setStatusId(2L); // 2=仅收藏
            newRecord.setCollectTime(now);
            appMediumActivityUserMapper.insertAppMediumActivityUser(newRecord);
            return 1; // 新增
        } else {
            // 有记录：更新状态（若已报名则改为3，否则改为2）
            Long newStatus = (existing.getStatusId() == 1L) ? 3L : 2L; // 1=仅参与→3=既参与且收藏
            existing.setStatusId(newStatus);
            existing.setCollectTime(now); // 更新收藏时间
            appMediumActivityUserMapper.updateStatusByUserIdAndActivityId(existing);
            return 2; // 更新
        }
    }
}
